/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.pictual.controller.mainPackage;

import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
//import java.System.out.*;

/**
 *
 *
 */
public class GermanWikiDicParser extends WikiDicParser {

    private static List<String> _types = Arrays.asList(
            "Teilchen",
            "Substantiv",
            "Pr.C3.A4position",
            "Verb",
            "Hilfsverb",
            "Adjektiv",
            "Buchstabe",
            "Konjunktion",
            "Konjugierte_Form",
            "Grammatische Merkmale",
            "Abk.C3.3Frzung",
            "Deklinierte_Form");

    protected boolean isLang(String language) {
        return Language.isLang(0, language);
    }

    protected boolean IsType(String word) {
        return _types.contains(word);
    }

    public String WikiLang(int i) {
    	System.out.println("WIKILANG:"+i);
        return "German";
    }

    public void extractAudio(String line, DefinitionList listdef){
        //line = line.substring(line.indexOf("href"));
        //String id = line.substring(line.indexOf("Audio"), line.indexOf("</td>"));
        String extension;
        int index = line.indexOf(".ogg");
        if(index < 0){
            index = line.indexOf(".OGG");
            extension = ".OGG";
            if(index < 0){
                return;
            }
        } else{
            extension = ".ogg";
        }
        String link = line.substring(line.indexOf("http"), index);
        link = "href=\""+link+extension+"\" class=\"internal\" title=\"Audio"+listdef.getAudios().size()+"\" target='audioZone'";
        System.out.println("AUDIO: "+link);
        listdef.addAudio(link);
    }

    public void extractPronunciation(String line, DefinitionList listdef){
        String tmpPronunciation = "<br><br>";
        int index1 = line.indexOf("<dd>");
        int index2 = line.indexOf("</dd>");
        while(index1 >=0 && index2 >=0){
            String element = line.substring(index1, index2);
            if(element.contains("IPA") && tmpPronunciation.length() < 9){
                String ref = element.substring(element.indexOf("[")+1);
                ref = ref.substring(0, ref.indexOf("]"));
                while(ref.indexOf("<") >= 0){
                    ref = ref.replace(ref.substring(ref.indexOf("<"), ref.indexOf(">") + 1), "");
                }
                if(!ref.equals("...")){
                    tmpPronunciation += ref;
                    //System.out.println("Ex:"+ref);
                    listdef.setPronunciation(tmpPronunciation);
                }
            }
                line = line.substring(index2+5);
                index1 = line.indexOf("<dd>");
                /*if(index1 >=0){
                    line = line.substring(index1);
                }*/
                index2 = line.indexOf("</dd>");
                if(index1 > index2){
                    line = line.substring(index2+5);
                    index1 = line.indexOf("<dd>");
                    index2 = line.indexOf("</dd>");
                }
        }
    }

    public void extractDefinitions(String line, DefinitionList listdef){
        String tmpDefinition;
        int index1 = line.indexOf("<dd>");
        int index2 = line.indexOf("</dd>")+5;
        while(index1 >=0 && index2 >=0){
            String element = line.substring(index1, index2);
            if(element.length() > 12){
                tmpDefinition = element.substring(8, element.length()-5);
                listdef.addDefinition(tmpDefinition);
            }            
            line = line.substring(index2);
                index1 = line.indexOf("<dd>");
                index2 = line.indexOf("</dd>")+5;
                if(index1 > index2){
                    line = line.substring(index2+5);
                    index1 = line.indexOf("<dd>");
                    index2 = line.indexOf("</dd>")+5;
                }
         }
    }

    public void extractDefinitions2(String line, DefinitionList listdef){
        String tmpDefinition;
        int index1 = line.indexOf("<li>")+4;
        int index2 = line.indexOf("</li>");
        while(index1 >=0 && index2 >=0){
            tmpDefinition = line.substring(index1, index2);
            listdef.addDefinition(tmpDefinition);
            line = line.substring(index2+5);
                index1 = line.indexOf("<li>");
                index2 = line.indexOf("</li>");
                if(index1 > index2){
                    line = line.substring(index2+5);
                    index1 = line.indexOf("<li>");
                    index2 = line.indexOf("</li>");
                }
         }
    }

    public int extractExamples(String line, DefinitionList listdef, int count){
        String tmpExample = "";
        int index1 = line.indexOf("<dd>");
        int index2 = line.indexOf("</dd>")+5;
        while(index1 >=0 && index2 >=0){
            String element = line.substring(index1, index2);
            tmpExample = "";
            if(!element.contains("[")){
                tmpExample = element.substring(5, element.length()-5);
            } else{
                if(element.length() > 12){
                    tmpExample = element.substring(8, element.length()-5);
                }
            }
            if(!tmpExample.equals("")){
                listdef.addExampleGerman(count+tmpExample);
                count++;
            }           
                line = line.substring(index2);
                index1 = line.indexOf("<dd>");
                index2 = line.indexOf("</dd>")+5;
                if(index1 > index2){
                    line = line.substring(index2+5);
                    index1 = line.indexOf("<dd>");
                    index2 = line.indexOf("</dd>")+5;
                }
        }
        return count;
    }

    public void extractGender(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("<em"), line.indexOf("</em>"));
            String gender = "";
            char option = line2.charAt(line2.length() - 1);
            switch(option){
                case 'm':
                    gender = "der";
                    break;
                case 'f':
                    gender = "die";
                    break;
                case 'n':
                    gender = "das";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public void extractGenderFrench(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("<em"), line.indexOf("</em>"));
            String gender = "";
            char option = line2.charAt(line2.length() - 1);
            switch(option){
                case 'm':
                    gender = "le";
                    break;
                case 'f':
                    gender = "la";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public void extractGenderSpanish(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("<em"), line.indexOf("</em>"));
            String gender = "";
            char option = line2.charAt(line2.length() - 1);
            switch(option){
                case 'm':
                    gender = "el";
                    break;
                case 'f':
                    gender = "la";
                    break;
            }
            listdef.setGender(gender);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    public void extractPlural(String line, DefinitionList listdef){
        try{
            String line2 = line.substring(line.indexOf("title=\"Hilfe:Kasus"));
            line2 = line2.substring(line2.indexOf("</tr>")+5);
            line2 = line2.substring(line2.indexOf("<tr"), line2.indexOf("</tr>"));

            // buscar en los td el plural
            // falla esto y haus los ejemplos
            
            int index1 = line2.indexOf("<td");
            int index2 = line2.indexOf("</td>")+5;
            String element = "";
            while(index1 >=0 && index2 >=0){
                element = line2.substring(index1, index2);
                element = element.substring(4, element.length() - 5);
                line2 = line2.substring(index2);
                index1 = line2.indexOf("<td");
                index2 = line2.indexOf("</td>")+5;
                if(index1 > index2){
                    line2 = line2.substring(index2+5);
                    index1 = line2.indexOf("<td");
                    index2 = line2.indexOf("</td>")+5;
                }
        }            
            listdef.setPlural(element);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    protected void extractEnglish(String line, DefinitionList listdef){
        if(line.contains("Aussprache:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Aussprache:"));
                    line2 = line2.substring(0, line2.indexOf("</dl>"));
                    extractPronunciation(line2, listdef);
        }
        if(line.contains("H%C3%B6rbeispiele")){
                    System.out.println("Hay sonido");
                    String line2 = line.substring(line.indexOf("H%C3%B6rbeispiele"));
                    line2 = line2.substring(0, line2.indexOf("</dd>"));
                    extractAudio(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i))+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        int count = 1;
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("Bedeutungen:"));
                line2 = line2.substring(line2.indexOf("<dl>"), line2.indexOf("</dl>")+5);
                extractDefinitions(line2, listdef);
                line2 = line.substring(line.indexOf("Beispiele:"));
                int index = line2.indexOf("<dl>");
                if(index < 20){
                    line2 = line2.substring(index, line2.indexOf("</dl>")+5);
                    count = extractExamples(line2, listdef, count);
                }
            }
        }
    }

    protected void extractSpanish(String line, DefinitionList listdef){
        if(line.contains("Aussprache:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Aussprache:"));
                    line2 = line2.substring(0, line2.indexOf("</dl>"));
                    extractPronunciation(line2, listdef);
        }
        if(line.contains("H%C3%B6rbeispiele")){
                    System.out.println("Hay sonido");
                    String line2 = line.substring(line.indexOf("H%C3%B6rbeispiele"));
                    line2 = line2.substring(0, line2.indexOf("</dd>"));
                    extractAudio(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i));
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        int count = 1;
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("Bedeutungen:"));
                line2 = line2.substring(line2.indexOf("<dl>"), line2.indexOf("</dl>")+5);
                extractDefinitions(line2, listdef);
                line2 = line.substring(line.indexOf("Beispiele:"));
                int index = line2.indexOf("<dl>");
                if(index < 20){
                    line2 = line2.substring(index, line2.indexOf("</dl>")+5);
                    count = extractExamples(line2, listdef, count);
                }
                line2 = line.substring(indexVector[i]);
                if(line2.contains("id=\"Substantiv") || line2.contains("id=\"Deklinierte_Form")){
                    extractGenderSpanish(line2, listdef);
                }
            }
        }
        if(listdef.isPlural()){
            if(listdef.getGender().equals("la")){
                listdef.setGender("las");
            } else{
                listdef.setGender("los");
            }
        }
    }

    protected void extractGerman(String line, DefinitionList listdef){
        if(line.contains("Aussprache:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Aussprache:"));
                    line2 = line2.substring(0, line2.indexOf("</dl>"));
                    extractPronunciation(line2, listdef);
        }
        if(line.contains("H%C3%B6rbeispiele")){
                    System.out.println("Hay sonido");
                    String line2 = line.substring(line.indexOf("H%C3%B6rbeispiele"));
                    line2 = line2.substring(0, line2.indexOf("</dd>"));
                    extractAudio(line2, listdef);
        }
        if(line.contains("Grammatische Merkmale:")){
                    String line2 = line.substring(line.indexOf("Grammatische Merkmale:"));
                    line2 = line2.substring(line2.indexOf("<ul>"), line2.indexOf("</ul>")+5);
                    extractDefinitions2(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = -1;
             String type = "id=\""+_types.get(i);
            while(line2.contains(type)){
                index += line2.indexOf(type)+1;
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        int count = 1;
           for(int i=0; i < indexVector.length; i++){
               if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("Bedeutungen:"));
                line2 = line2.substring(line2.indexOf("<dl>"), line2.indexOf("</dl>")+5);
                extractDefinitions(line2, listdef);
                line2 = line.substring(line.indexOf("Beispiele:"));
                int index = line2.indexOf("<dl>");
                if(index < 20){
                    line2 = line2.substring(index, line2.indexOf("</dl>")+5);
                    count = extractExamples(line2, listdef, count);
                }
                line2 = line.substring(indexVector[i]);
                if(line2.contains("id=\"Substantiv") || line2.contains("id=\"Deklinierte_Form")){
                    extractGender(line2, listdef);
                    extractPlural(line2, listdef);
                }
            }
        }
        if(listdef.isPlural()){
            listdef.setGender("pl. die");
        }
    }

    protected void extractFrench(String line, DefinitionList listdef){
        if(line.contains("Aussprache:")){
                    System.out.println("Hay pronunciacion");
                    String line2 = line.substring(line.indexOf("Aussprache:"));
                    line2 = line2.substring(0, line2.indexOf("</dl>"));
                    extractPronunciation(line2, listdef);
        }
        if(line.contains("H%C3%B6rbeispiele")){
                    System.out.println("Hay sonido");
                    String line2 = line.substring(line.indexOf("H%C3%B6rbeispiele"));
                    line2 = line2.substring(0, line2.indexOf("</dd>"));
                    extractAudio(line2, listdef);
        }
        int[] indexVector = new int[_types.size()];
        for(int i = 0; i < indexVector.length; i++){
            indexVector[i] = -1;
        }
         int order = 0;
         for(int i=0; i< _types.size(); i++){
             String line2 = line;
             int index = 0;
            while(line2.contains("id=\""+_types.get(i))){
                index += line2.indexOf("id=\""+_types.get(i));
                indexVector[order] = index;
                if(_types.get(i).startsWith("Verb"))
                    listdef.setVerb(Boolean.TRUE);
                order++;
                line2 = line.substring(index+1);
             }
        }
        //ordenar vector
        boolean change = true;
        while(change){
            change = false;
            for(int i = 0; i < indexVector.length-1; i++){
                if(indexVector[i] > indexVector[i+1]){
                    int aux = indexVector[i];
                    indexVector[i] = indexVector[i+1];
                    indexVector[i+1] = aux;
                    change = true;
                }
            }
        }
        int count = 1;
        for(int i=0; i < indexVector.length; i++){
            if(indexVector[i] >= 0){
                String line2 = line.substring(indexVector[i]);
                line2 = line2.substring(line2.indexOf("Bedeutungen:"));
                line2 = line2.substring(line2.indexOf("<dl>"), line2.indexOf("</dl>")+5);
                extractDefinitions(line2, listdef);
                line2 = line.substring(line.indexOf("Beispiele:"));
                int index = line2.indexOf("<dl>");
                if(index < 20){
                    line2 = line2.substring(index, line2.indexOf("</dl>")+5);
                    count = extractExamples(line2, listdef, count);
                }
                line2 = line.substring(indexVector[i]);
                if(line2.contains("id=\"Substantiv") || line2.contains("id=\"Deklinierte_Form")){
                    extractGenderFrench(line2, listdef);
                }
            }
        }
        if(listdef.isPlural()){
            listdef.setGender("les");
        }
    }

    protected boolean extractDefinitionList(BufferedReader input, DefinitionList listdef, int lang) {

        boolean end = false;
        boolean find = false;
        String languaje = Language.LanStr(2, lang);

        String line;

        try {
        	System.out.println("--------------------------------------");
        	System.out.println("we are "+languaje+" to german");
        	System.out.println("--------------------------------------");

            while (!end && (line = input.readLine()) != null) {
            	if(line.contains("Wiktionary:" + languaje))
            	{
            		System.out.println("German>>>"+line+"\n");
                        String line2 = "";
                        line = input.readLine();
                        while(line != null && !line.startsWith("<h2>")){
                            line2 += line;
                            line = input.readLine();
                        }
                        end = true;
                        find = true;
                        switch(lang){
                            case 0:
                                extractEnglish(line2, listdef);
                                break;
                            case 1:
                                extractSpanish(line2, listdef);
                                break;
                            case 2:
                                extractGerman(line2, listdef);
                                break;
                            case 3:
                                extractFrench(line2, listdef);
                                break;
                            default:
                                find = false;
                        }
            	}
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return find;
    }
}
